with GNAT.IO; use GNAT;
with Percentages;

--
--  implement encapsulation methods against 'Website_Prototype' instance of
--       'Website' defined in specifications
--

package body Websites_Prototypes is

   -- defined in Websites_Prototypes specifications

   --     use Websites_Pkg_Proto;


   -- defined in Websites

   package Contents_T renames Contents_Type_Pkg;

   Data : Website renames Website_Prototype.Website_Record;


   --
   -- constructor
   --

   overriding
   procedure Initialize( Website_Prototype : in out Website_Prototype_Type )

   is
      -- rename descriptive argument with a concise pointer

      type Prototype_TA is access all Website_Prototype_Type;
      Proto : Prototype_TA := Website_Prototype'Access;

   begin

      Contents_T.Append( Proto.Website_Record.Contents, "" );

      IO.Put_Line("Websites Prototype instance initialization using: "

                       & Ascii.LF

                       & "Content_Size_Max: "

                       & Positive'Image( Content_Size_Max )

                       & Ascii.LF

                       & "URL_Length_Max: "

                       & Positive'Image( URL_Length_Max )

                       & Ascii.LF );

   end Initialize;


   --
   -- destructor
   --

   overriding
   procedure Finalize( Website_Prototype : in out Website_Prototype_Type )

   is
      -- rename descriptive argument with a concise pointer

      type Prototype_TA is access all Website_Prototype_Type;
      Proto : constant Prototype_TA := Website_Prototype'Access;

      Len : constant Integer := Contents_T.Length(

         Proto.Website_Record.Contents );

      Len_Str : constant String   := Integer'Image( Len );

      Max     : constant Positive := Websites_Pkg_Parameter.Content_Size_Max;

      Max_Str : constant String   := Positive'Image(Max);

      type Precision_Type is delta 0.01 digits 16;

      package Percentage_T is new Percentages( Precision_Type,
                                               Precision_Type );

      use Percentage_T;

   begin

      IO.Put_Line( Ascii.LF
                        & "Destruction of Websites Prototype instance: "
                        & Ascii.LF
                        & "Bounded String Space Used for Contents: "
                        & Len_Str
                        & " of "
                        & Max_Str
                        & " = "
                        & Percentage_Type_Int'Image( Percentage( Len, Max ))
                        & "%");

      if( Len = 0 ) then

         return;     -- nothing to delete

      end if;

      Contents_T.Delete( Data.Contents,

                         1,

                         Len );

   end Finalize;


   --
   -- disable warnings about interface parameter unused
   --

   pragma Warnings (Off, "formal parameter ""Website_Instance"" is not referenced");

   --
   -- get contents
   --
   overriding
   function Get_Contents( Website_Instance : in Website_Object_Interface )

         return Contents_Str_Sub_Type
   is
   begin

      return Contents_Type_Pkg.To_String( Data.Contents );

   end Get_Contents;


   --
   -- set contents
   --
   overriding
   procedure Set_Contents( Website_Instance : in out Website_Object_Interface;
                           Website_Contents : in Contents_Str_Sub_Type;
                           Append : in Boolean := false )
   is

   begin

      if( not Append ) then

         Erase( Website_Instance );

      end if;

      Contents_Type_Pkg.Append( Data.Contents,

                                Website_Contents );


   end Set_Contents;


   --
   -- size of bounded string contents consumed
   --
   overriding
   function Size( Website_Instance : in Website_Object_Interface )

         return Contents_Int_Sub_Type

   is
   begin

      return Contents_Type_Pkg.Length( Data.Contents );

   end Size;


   --
   -- erase
   --
   overriding
   procedure Erase( Website_Instance : in out Website_Object_Interface )
   is

      Len : constant Integer := Contents_T.Length( Data.Contents );

   begin

      if( Len = 0 ) then

         return;     -- nothing to delete

      end if;

      Contents_Type_Pkg.Delete( Data.Contents,

                                1,

                                Len );

   end Erase;


   --
   -- set url
   --
   overriding
   procedure Set_URL( Website_Instance : in out Website_Object_Interface;
                      URL : in URL_Str )
   is

      Len : constant Integer := Contents_T.Length( Data.Contents );

   begin

      URL_Type_Pkg.Delete( Data.URL,

                           1,

                           Len );

      URL_Type_Pkg.Append( Data.URL,

                           URL );

   end Set_URL;


   --
   -- get url
   --
   overriding
   function Get_URL( Website_Instance : in Website_Object_Interface )

         return URL_Str

   is
   begin

      return URL_Type_Pkg.To_String( Data.URL );

   end Get_URL;


   --
   -- percentage of bounded string space used for website contents
   --
   overriding
   function Percentage_Space_Used( Website_Instance : in

         Website_Object_Interface )

         return Integer

   is

      Len : constant Integer := Contents_T.Length( Data.Contents );

      type Precision_Type is delta 0.01 digits 8;

      package Percentage_T is new Percentages( Precision_Type,
                                               Precision_Type );

   begin

         return Percentage_T.Percentage( Len, Content_Size_Max );

   end Percentage_Space_Used;


   --
   -- to satisfy compiler
   --       re-enable warnings about interface parameter unused
   --

   pragma Warnings (On, "formal parameter ""Website_Instance"" is not referenced");


end Websites_Prototypes;